<template>
  <div class="subjectsTable">
    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      element-loading-text="给我一点时间"
      :data="subjectTableData"
       :header-cell-style="{background:'#FAFAFA'}"
        fit
        highlight-current-row
      style="width: 100%;margin:15px 0">
      <el-table-column type="index" label="序号" width="80"></el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="150"></el-table-column>
      <el-table-column prop="username" label="创建者" width="180"></el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="180">
        <template slot-scope="{row}">
          {{row.addDate|formatDate}}
        </template>
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示" >
        <template slot-scope="{row}">
          {{row.isFrontDisplay===1?'是':'否'}}
        </template>
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录" ></el-table-column>
      <el-table-column prop="tags" label="标签" ></el-table-column>
      <el-table-column prop="totals" label="题目数量" ></el-table-column>
      <el-table-column label="操作" width="240">
        <template slot-scope="{row}">
          <el-button type="text" @click="$router.push({path:'/subjects/directorys',query: {id:row.id,name:row.subjectName }})">学科分类</el-button>
          <el-button type="text" @click="$router.push({path:'/subjects/tags',query:{id:row.id,name:row.subjectName }})">学科标签</el-button>
          <el-button type="text" @click="onEdit(row)">修改</el-button>
          <el-button type="text" @click="onRemove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域 -->
    <!-- 分页区域 -->
   <el-row type="flex" class="row-bg" justify="end">
     <el-col :span="7">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="10"
          layout=" prev, pager, next,sizes, jumper"
          background
          :total="total">
    </el-pagination>
     </el-col>
   </el-row>
    <!-- 分页区域 -->
    <!-- 修改弹出层 -->
    <subjects-add
    :addSubjectDialogVisible.sync="addSubjectDialogVisible"
    :form="form"
    @on-save="onSave"
  @on-close="onClose"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, remove } from '@/api/hmmm/subjects.js'
import subjectsAdd from '../subjects-add.vue'
export default {
  components: { subjectsAdd },
  name: 'subjectsTable',
  props: {
    data: {
      type: Object,
      required: true
    },
    form: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      loading: true,
      subjectTableData: [],
      total: 0,
      addSubjectDialogVisible: false
    }
  },
  computed: {

  },
  filters: {
    formatDate (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    this.getSubjectTableList()
  },

  methods: {
    // 获取学科列表
    async  getSubjectTableList () {
      try {
        const res = await list(this.data)
        // console.log(res)
        this.subjectTableData = res.data.items
        this.total = res.data.counts
        // 把总数量传给父组件
        this.$emit('get-total', this.total)
        this.loading = false
      } catch (err) {
        console.log(err)
      }
    },
    // 改变每页多少条
    handleSizeChange (val) {
      this.data.pagesize = val
      this.getSubjectTableList()
    },
    // 改变当前页
    handleCurrentChange (val) {
      this.data.page = val
      this.getSubjectTableList()
    },
    // 点击修改
    onEdit (row) {
      this.addSubjectDialogVisible = true
      this.form.subjectName = row.subjectName
      this.form.isFrontDisplay = row.isFrontDisplay.toString()
      this.form.id = row.id
    },
    // 点击弹出层的确定，重新渲染列表
    onSave () {
      this.getSubjectTableList()
    },

    // 点击删除
    onRemove (row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (this.subjectTableData.length === 1 && this.data.page !== 1) {
          this.data.pagesize--
        }
        await remove({
          id: row.id
        })
        this.getSubjectTableList()
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 关闭弹层，清空输入框数据
    onClose () {
      this.$emit('on-close')
    }
  }
}
</script>

<style scoped lang='less'>

</style>
